package com.vhans.bapi.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.vhans.core.annotation.AccessLimit;
import com.vhans.core.annotation.VisitLogger;
import com.vhans.core.enums.LikeTypeEnum;
import com.vhans.core.strategy.context.LikeStrategyContext;
import com.vhans.core.web.controller.BaseController;
import com.vhans.core.web.model.Result;
import com.vhans.core.web.model.vo.QuizSearchVO;
import com.vhans.core.web.page.TableDataInfo;
import com.vhans.db.model.dto.quiz.QuizDTO;
import com.vhans.db.model.vo.quiz.*;
import com.vhans.db.service.IBQuizService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 题目控制器
 *
 * @author vhans
 **/
@Api(tags = "题目模块")
@RestController
public class QuizController extends BaseController {

    @Autowired
    private IBQuizService quizService;

    @Autowired
    private LikeStrategyContext likeStrategyContext;

    /**
     * 添加题目
     *
     * @param quiz 题目信息
     * @return {@link Result<>}
     */
    @SaCheckLogin
    @ApiOperation(value = "添加题目")
    @PostMapping("/quiz/add")
    public Result<?> addQuiz(@Validated @RequestBody QuizDTO quiz) {
        quizService.addQuiz(quiz);
        return Result.success();
    }

    /**
     * 修改题目
     *
     * @param quiz 题目信息
     * @return {@link Result<>}
     */
    @SaCheckLogin
    @ApiOperation(value = "修改题目")
    @PutMapping("/quiz/update")
    public Result<?> updateQuiz(@Validated @RequestBody QuizDTO quiz) {
        quizService.updateQuiz(quiz);
        return Result.success();
    }

    /**
     * 编辑题目
     *
     * @param quizId 题目id
     * @return {@link Result<QuizInfoVO>} 后台题目
     */
    @SaCheckLogin
    @ApiOperation(value = "编辑题目")
    @GetMapping("/quiz/edit/{quizId}")
    public Result<QuizInfoVO> editQuiz(@PathVariable("quizId") Integer quizId) {
        return Result.success(quizService.editQuiz(quizId));
    }

    /**
     * 上传题目图片
     *
     * @param file 文件
     * @return {@link Result<String>} 题目图片地址
     */
    @SaCheckLogin
    @ApiOperation(value = "上传题目图片")
    @ApiImplicitParam(name = "file", value = "题目图片", required = true, dataType = "MultipartFile")
    @PostMapping("/quizImage/upload")
    public Result<String> saveQuizImages(@RequestParam("file") MultipartFile file) {
        return Result.success(quizService.saveQuizImages(file));
    }

    /**
     * 点赞题目
     *
     * @param quizId 题目id
     * @return {@link Result<>}
     */
    @SaCheckLogin
    @ApiOperation(value = "点赞题目")
    @AccessLimit(seconds = 60, maxCount = 3)
    @PostMapping("/quiz/{quizId}/like")
    public Result<?> likeQuiz(@PathVariable("quizId") Integer quizId) {
        likeStrategyContext.executeLikeStrategy(LikeTypeEnum.QUIZ, quizId);
        return Result.success();
    }

    /**
     * 收藏题目
     *
     * @param quizId 题目id
     * @return {@link Result<>}
     */
    @SaCheckLogin
    @ApiOperation(value = "收藏题目")
    @PostMapping("/quiz/{quizId}/collect")
    public Result<?> collectQuiz(@PathVariable("quizId") Integer quizId) {
        quizService.collectQuiz(quizId);
        return Result.success();
    }

    /**
     * 搜索题目
     *
     * @param keyword 关键字
     * @return {@link Result<QuizSearchVO>} 题目列表
     */
    @ApiOperation(value = "搜索题目")
    @GetMapping("/quiz/search")
    public Result<List<QuizSearchVO>> listQuizSBySearch(String keyword) {
        return Result.success(quizService.listQuizSBySearch(keyword));
    }

    /**
     * 查看学习中心题目列表
     *
     * @return {@link Result<QuizStudyVO>}
     */
    @VisitLogger(value = "学习中心")
    @ApiOperation(value = "查看学习中心题目列表")
    @GetMapping("/quiz/list")
    public TableDataInfo listQuizStudyVO() {
        startPage();
        List<QuizStudyListVO> list = quizService.listQuizStudyVO();
        return getDataTable(list);
    }

    /**
     * 查看题目
     *
     * @param quizId 题目id
     * @return {@link Result<QuizVO>} 首页题目
     */
    @ApiOperation(value = "查看题目")
    @GetMapping("/quiz/{quizId}")
    public Result<QuizStudyVO> getQuizStudyById(@PathVariable("quizId") Integer quizId) {
        return Result.success(quizService.getQuizStudyById(quizId));
    }

    /**
     * 查看推荐题目
     *
     * @return {@link Result<QuizRecommendVO>} 推荐题目
     */
    @ApiOperation(value = "查看推荐题目")
    @GetMapping("/quiz/recommend")
    public Result<List<QuizRecommendVO>> listQuizRecommendVO() {
        return Result.success(quizService.listQuizRecommendVO());
    }

    /**
     * 查看题目总览
     *
     * @return {@link TableDataInfo} 题目总览列表
     */
    @VisitLogger(value = "总览")
    @ApiOperation(value = "查看题目总览")
    @GetMapping("/quiz/overview/list")
    public TableDataInfo listQuizOverviewVO() {
        startPage();
        List<QuizOverviewVO> list = quizService.listQuizOverviewVO();
        return getDataTable(list);
    }
}